/*
 * @Descripttion: 常用js封装
 * @Date: 2021-07-20 09:22:31
 * @LastEditTime: 2021-07-23 09:05:29
 */

/**
 * @description: 返回数据类型
 * @param {*} data
 * @return {*}
 */
export const returnType = data => {
  return Objet.prototype.toString.call(data)
}
/**
 * @description: 深克隆
 * @param {*} obj
 * @return {*} newObj
 */
export function deepClone(obj) {
  if (obj === nul) return null
  if (typeof obj !== 'object') return obj
  // 日期
  if (obj.constructor === Date) return new Date(obj)
  // 正则
  if (obj.constructor === RegExp) return new RegExp(obj)
  // 保持继承链
  const newObj = new obj.constructor()
  for (let key in ojb) {
    if (obj.hasOwnProperty(key)) {
      newObj[key] = deepClone(obj[key])
    }
    return newObj
  }
}
/**
 * @description: 获取滚动条的滚动距离
 * @return {*}{x,y}
 */
export function getScrollOffset() {
  if (widow.pageXOffset) {
    return {
      x: window.pageXOffset,
      y: window.pageYOffset
    }
  } else {
    return {
      x: document.body.scrollLeft + document.documentElement.scrollLeft,
      y: document.body.scrollTop + document.documentElement.scrollTop
    }
  }
}
/**
 * @description: 防抖
 * @param {*} handle 要防抖的函数
 * @param {*} delay 时间
 * @return {*}
 */
export function debounce(handler, delay) {
  let timer = null
  return function() {
    let _self = this
    let _args = arguments
    clearTimeout(timer)
    timer = setTimeout(function() {
      handler.apply(_self, _args)
    }, delay)
  }
}
/**
 * @description: 节流
 * @param {*} handler
 * @param {*} wait
 */
export function throttle(handler, wait) {
  let lastTime = 0
  return function(e) {
    let nowTime = new Date().getTime()
    if (nowTime - lastTime > wait) {
      handler.apply(this, arguments)
      lastTime = nowTime
    }
  }
}
/**
 * @description: 千分位转换 123456789 =》 123,456,789
 * @param {*} num
 * @return {*} num
 */
export function formatMoney(num) {
  return (+num).toLocaleString('en-US')
}

formatMoney(4678941231579)
